<template>
  <view class="basis-page">
    <view class="basis-container com_container">
      <view class="essential-info">填写基本信息</view>
      <u-form labelPosition="left" :model="formData" ref="uForm" :borderBottom='false' :rules='rules'>
        <u-form-item prop="nameValue">
          <view class="ipts mar-top90">
            <view class="ipt-box">
              <input class="ipt" v-model="formData.nameValue" type="text" placeholder="请输入姓名"
                placeholder-style="font-size: 28rpx; color: #666666;" />
            </view>
          </view>
        </u-form-item>
        <u-form-item prop="workUnit">
          <view class="ipts">
            <view class="ipt-box">
              <input class="ipt" v-model="formData.workUnit" type="text" placeholder="请输入单位名称"
                placeholder-style="font-size: 28rpx; color: #666666;" />
            </view>
          </view>
        </u-form-item>
        <u-form-item prop="location">
          <view class="ipts">
            <view class="ipt-box">
              <input class="ipt" v-model="formData.location" type="text" placeholder="请输入单位地址"
                placeholder-style="font-size: 28rpx; color: #666666;" />
            </view>
          </view>
        </u-form-item>
        <u-form-item prop="address">
          <view class="ipts">
            <view class="ipt-box">
              <picker mode='region' @change="chooseregion" style="width: 100%;">
                <input class="ipt" v-model="formData.address" disabled placeholder="请选择具体地址" />
              </picker>
              <view class="arrow-right">
                <u-icon name="arrow-down" color="#333333" size="16"></u-icon>
              </view>
            </view>
          </view>
        </u-form-item>
      </u-form>


      <!-- <view class="ipts"> -->
      <!-- <view class="ipt-box">
          <input class="ipt" v-model="formData.nameValue" type="text" placeholder="请输入姓名"
            placeholder-style="font-size: 28rpx; color: #666666;">
        </view> -->




      <button class="con_button btn" @click="accomplish">完成</button>

      <!-- </view> -->
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        formData: {
          nameValue: '', //名字
          workUnit: '', //所属单位
          location: '', //单位地址
          province: '', //省
          city: '', //城市
          region: '', //街区
          address: ''
        },
        mobile: '',
        roleId: '',
        rules: {
          nameValue: [{
            type: 'string',
            required: true,
            message: '请填写姓名',
            trigger: ['blur', 'change']
          }],
          workUnit: [{
            type: 'string',
            required: true,
            message: '请填写单位名称',
            trigger: ['blur', 'change']
          }],
          location: [{
            type: 'string',
            required: true,
            message: '请填写单位地址',
            trigger: ['blur', 'change']
          }],
          address: [{
            type: 'string',
            required: true,
            message: '请选择具体地址',
            trigger: ['blur', 'change']
          }],
        },
      };
    },
    onReady() {
      //如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
      this.$refs.uForm.setRules(this.rules)
    },
    onLoad(query) {
      console.log(query);
      this.roleId = query.roleId
      this.mobile = query.phone
    },

    // computed: {
    //   address() {
    //     if (this.formData.province && this.formData.city && this.formData.region) {
    //       return this.formData.province + '-' + this.formData.city + '-' + this.formData.region
    //     } else {
    //       return ''
    //     }
    //   }
    // },
    methods: {
      // 获取省市区
      chooseregion(event) {
        console.log(event.detail.value);
        this.formData.province = event.detail.value[0]
        this.formData.city = event.detail.value[1]
        this.formData.region = event.detail.value[2]
        console.log(this.address);
        if (this.formData.province && this.formData.city && this.formData.region) {
          this.formData.address = this.formData.province + '-' + this.formData.city + '-' + this.formData.region
        } else {
          return ''
        }
      },
      async accomplish() {
        this.$refs.uForm.validate().then(async () => {
          // if (this.validateInputs()) {
          // 校验通过，可以执行后续逻辑，比如提交表单数据
          const res = await this.$http.post('/yaolexue-service-portal/sysPortalUser/registerPortalUser', {
            name: this.formData.nameValue,
            roleId: this.roleId,
            phoneNumber: this.mobile,
            location: this.formData.location,
            workUnit: this.formData.workUnit,
            province: this.formData.province,
            city: this.formData.city,
            region: this.formData.region
          }, {
            showErrorMsg: true,
            isNeedLoading: false
          })
          console.log('accomplish', res);
          if (res.code === 200) {
            if (res.data) {
              uni.setStorageSync('token', res.data)
			  this.getShareCode()
              uni.switchTab({
                url: '/pages/home/home'
              })
            }
          }
          // } else {
          //   // 校验未通过，可以提示用户或执行其他处理
          //   console.log('信息校验未通过');
          // }
        }).catch(errors => {})

      },
      // validateInputs() {
      //   // 校验逻辑
      //   if (!this.formData.nameValue || !this.formData.workUnit || !this.formData.location || !this.formData.province) {
      //     uni.showToast({
      //       title: '请输入相关内容',
      //       icon: 'none',
      //     });
      //     return false;
      //   }

      //   return true;
      // }
	  
	  getShareCode(){
	  	let shareUserId = getApp().globalData.uid
	  	if(shareUserId){
	  		this.$http.post(`/yaolexue-service-portal/sysPortalUser/shareCode/${shareUserId}`,{
	  			
	  		}).then(res => {
	  			
	  		})
	  	}
	  }
    }
  }
</script>

<style lang="scss" scoped>
  .basis-page {
    .basis-container {
      .essential-info {
        font-size: 48rpx;
        font-family: SimSun;
        font-weight: 400;
        color: #333333;
        -webkit-text-stroke: 1px #333333;
        text-stroke: 1px #333333;
        margin-top: 100rpx;
      }

      .ipts {

        .ipt-box {
          width: 561rpx;
          height: 90rpx;
          background: #E1E1E1;
          opacity: 0.8;
          border-radius: 45rpx;
          // margin-bottom: 50rpx;
          display: flex;
          justify-content: space-between;
          align-items: center;
          overflow: hidden;

          .ipt {
            margin-left: 37rpx;
          }

          .arrow-right {
            margin-right: 45rpx;

          }

        }
      }

      .btn {
        margin-top: 160rpx;
      }
    }
  }
</style>